package com.gsafety.libraryktx.util

import android.content.Context
import androidx.recyclerview.widget.*
import com.gsafety.libraryktx.R

/**
 * @author : lj
 * @date : 2020/7/21 17:49
 */
object  RecyclerViewHelper {

    /**
     * 配置垂直列表RecyclerView
     *
     * @param view
     */
    fun initRecyclerViewV(context: Context?, view: RecyclerView, isDivided: Boolean,
                          adapter: RecyclerView.Adapter<*>?) {
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        view.setHasFixedSize(true)
        view.layoutManager = layoutManager
        view.itemAnimator = DefaultItemAnimator()
        if (isDivided) {
            var dv=DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
            dv.setDrawable(context!!.resources.getDrawable(R.drawable.common_divider_v));
            view.addItemDecoration(dv)
        }
        view.adapter = adapter
    }

    fun initRecyclerViewV(context: Context?, view: RecyclerView, adapter: RecyclerView.Adapter<*>?) {
        initRecyclerViewV(context, view, true, adapter)
    }

    /**
     * 配置垂直列表RecyclerView(GridLayout)
     *
     * @param view
     */
    fun initRecyclerViewH(context: Context?, view: RecyclerView, spanCount: Int,
                          adapter: RecyclerView.Adapter<*>?) {
        val layoutManager = GridLayoutManager(context, spanCount)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        view.setHasFixedSize(true)
        view.layoutManager = layoutManager
        view.itemAnimator = DefaultItemAnimator()
        view.adapter = adapter
    }

    fun initRecyclerViewH(context: Context?, view: RecyclerView, adapter: RecyclerView.Adapter<*>?) {
        initRecyclerViewH(context, view, true, adapter)
    }

    /**
     * 配置水平列表RecyclerView
     *
     * @param view
     */
    fun initRecyclerViewH(context: Context?, view: RecyclerView, isDivided: Boolean,
                          adapter: RecyclerView.Adapter<*>?) {
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        view.layoutManager = layoutManager
        view.itemAnimator = DefaultItemAnimator()
        if (isDivided) {
            view.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL))
        }
        view.adapter = adapter
    }

    /**
     * 配置瀑布流列表RecyclerView
     *
     * @param view
     */
    fun initRecyclerViewSV(context: Context?, view: RecyclerView, isDivided: Boolean,
                           adapter: RecyclerView.Adapter<*>?, column: Int) {
        val layoutManager = StaggeredGridLayoutManager(column, StaggeredGridLayoutManager.VERTICAL)
        view.layoutManager = layoutManager
        view.itemAnimator = DefaultItemAnimator()
        if (isDivided) {
            view.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        }
        view.adapter = adapter
    }

    fun initRecyclerViewSV(context: Context?, view: RecyclerView, adapter: RecyclerView.Adapter<*>?, column: Int) {
        initRecyclerViewSV(context, view, false, adapter, column)
    }

}